package services

import (
	"app/common/constant"
	"app/common/response"
	"app/internal/codec"
	"app/internal/model"
	"app/internal/structs/requests"
	"app/pkg/log"
	"net/http"

	"github.com/gin-gonic/gin"
)

//	@Summary	获取配置
//	@Schemes
//	@Description
//	@Tags		系统配置
//	@Accept		json
//	@Param		payload	body		requests.SettingGetRequests						true	"body"
//	@success	200		{object}	response.JSONResult{data=[]model.SettingModel}	"desc"
//	@Router		/api/common/setting-get [POST]
func SystemGet(c *gin.Context) {
	SettingGet(c)
}

//	@Summary	获取配置
//	@Schemes
//	@Description
//	@Tags		系统配置
//	@Accept		json
//	@Param		payload	body		requests.SettingGetRequests						true	"body"
//	@success	200		{object}	response.JSONResult{data=[]model.SettingModel}	"desc"
//	@Router		/manager/setting/get [POST]
func SettingGet(c *gin.Context) {
	req := requests.SettingGetRequests{}
	if err := codec.GinBind(c, &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	data, err := model.NewSettingModel().GetInfoByNameBatch(req.Name)
	if err != nil {
		log.NewError("SettingGet", err.Error())
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "获取配置失败"})
		return
	}
	c.JSON(http.StatusOK, response.JSONResult{Data: data})
}

//	@Summary	设置配置
//	@Schemes
//	@Description
//	@Tags		系统配置
//	@Accept		json
//	@Param		payload	body		requests.SettingSetRequests						true	"body"
//	@success	200		{object}	response.JSONResult{data=[]model.SettingModel}	"desc"
//	@Router		/manager/setting/set [POST]
func SettingSet(c *gin.Context) {
	req := requests.SettingSetRequests{}
	if err := codec.GinBind(c, &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	err := model.NewSettingModel().SetBatch(req.Data)
	if err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}
